2 Problem: A - Angry programmer
3 Author: Andrés Mejía-Posada
4 (http://blogaritmo.factorcomun.org)
29 #define D(x) cout << #x " is " << x << endl
31 const int MAXN
= 2*55;
33 int cap
[MAXN
+1][MAXN
+1], flow
[MAXN
+1][MAXN
+1], prev
[MAXN
+1];
36 cap[i][j] = Capacidad de la arista (i, j).
37 flow[i][j] = Flujo actual de i a j.
38 prev[i] = Predecesor del nodo i en un camino de aumentación.
41 int fordFulkerson(int n
, int s
, int t
){
43 for (int i
=0; i
<n
; ++i
) fill(flow
[i
], flow
[i
]+n
, 0);
45 fill(prev
, prev
+n
, -1);
48 while (q
.size() && prev
[t
] == -1){
51 for (int v
= 0; v
<n
; ++v
)
52 if (v
!= s
&& prev
[v
] == -1 && cap
[u
][v
] - flow
[u
][v
] > 0 )
53 prev
[v
] = u
, q
.push(v
);
56 if (prev
[t
] == -1) break;
58 int bottleneck
= INT_MAX
;
59 for (int v
= t
, u
= prev
[v
]; u
!= -1; v
= u
, u
= prev
[v
]){
60 bottleneck
= min(bottleneck
, cap
[u
][v
] - flow
[u
][v
]);
62 for (int v
= t
, u
= prev
[v
]; u
!= -1; v
= u
, u
= prev
[v
]){
63 flow
[u
][v
] += bottleneck
;
64 flow
[v
][u
] = -flow
[u
][v
];
72 inline int in(int i
){ return 2*i
; }
73 inline int out(int i
){ return 2*i
+1; }
76 //assert(freopen("angry.in", "r", stdin) != NULL);
78 while (cin
>> n
>> e
&& (n
+e
)){
79 memset(cap
, 0, sizeof cap
);
81 //Las dos máquinas que no pueden ser destruídas.
82 cap
[in(0)][out(0)] = cap
[in(n
-1)][out(n
-1)]= INT_MAX
;
85 //Costo de destrucción de máquinas.
86 for (int k
=2, i
, w
; k
<n
; ++k
){
88 cap
[in(i
)][out(i
)] = w
;
91 //Costo de destrucción de cables.
92 for (int k
=0, i
, j
, w
; k
<e
; ++k
){
93 cin
>> i
>> j
>> w
; --i
, --j
;
94 cap
[out(i
)][in(j
)] = cap
[out(j
)][in(i
)] = w
;
96 int ans
= fordFulkerson(2*n
, in(0), out(n
-1));